<template>
    <div class="g-height100 g-width100">
        <!-- 列表与搜索 -->
    <TaskListComponent ref="taskListCom" bus-key="taskToDoManageList"
                       :table-operations="tableOperations"
                       model-type="runtime" :list-url="$baseURL.workflow +  '/workflow/task/tasks/todo/listPage'">
    </TaskListComponent>
        <!-- 任务分配弹窗 -->
        <TaskTransferDialog ref="taskTransferDialog" :task-id="taskTransferDialog.taskId"
                          :on-success="()=>{$refs.taskListCom.getTableRef().refreshData()}"></TaskTransferDialog>
        <!-- 任务委托弹窗 -->
        <TaskDelegateDialog ref="taskDelegateDialog" :task-id="taskDelegateDialog.taskId"
                            :on-success="()=>{$refs.taskListCom.getTableRef().refreshData()}"></TaskDelegateDialog>
        <!-- 任务驳回弹窗 -->
        <TaskRejectDialog ref="taskRejectDialog" :task-id="taskRejectDialog.taskId"
                            :on-success="()=>{$refs.taskListCom.getTableRef().refreshData()}"></TaskRejectDialog>
        <!-- 任务无表单办理弹窗 -->
        <TaskDealWithoutFormDialog ref="taskDealWithoutFormDialog" :task-id="taskDealWithoutFormDialog.taskId"
                          :on-success="()=>{$refs.taskListCom.getTableRef().refreshData()}"></TaskDealWithoutFormDialog>
    </div>
</template>

<script>
    let taskUnclaimButtonCode = 'taskUnclaimButtonCode'
    let taskResolveButtonCode = 'taskResolveButtonCode'
    let taskDealWithButtonCode = 'taskDealWithButtonCode'

    import TaskListComponent from '../../components/task/TaskListComponent.vue'
    import TaskTransferDialog from '../../components/task/TaskTransferDialog.vue'
    import TaskDelegateDialog from "../../components/task/TaskDelegateDialog.vue"
    import TaskRejectDialog from "../../components/task/TaskRejectDialog.vue"
    import TaskDealWithoutFormDialog from "../../components/task/TaskDealWithoutFormDialog.vue"
    export default {
        components:{
            TaskListComponent,
            TaskTransferDialog,
            TaskDelegateDialog,
            TaskRejectDialog,
            TaskDealWithoutFormDialog,
        },
        data(){
            return {
                tableOperations:[
                    {
                        action: this.showTaskTransferDialog,
                        disabledOnMissingSelect:true,
                        label:'任务转办'
                    },
                    {
                        action: this.showTaskDelegateDialog,
                        disabledOnMissingSelect:true,
                        label:'任务委托'
                    },
                    {
                        action: this.showTaskRejectDialog,
                        disabledOnMissingSelect:true,
                        label:'任务驳回'
                    },
                    {
                        action: this.taskResolve,
                        disabledOnMissingSelect:true,
                        disabled: (row)=>{
                            return !row.delegateState
                        },
                        code: taskResolveButtonCode,
                        label:'委托解决'
                    },
                    {
                        action: this.taskUnclaim,
                        disabledOnMissingSelect:true,
                        code: taskUnclaimButtonCode,
                        disabled:(row)=>{
                            return !row.claimTimeTxt
                        },
                        label:'反签收'
                    },
                    {
                        action: this.taskDealWith,
                        disabledOnMissingSelect:true,
                        code: taskDealWithButtonCode,
                        label:'办理'
                    }
                ],
                taskTransferDialog:{
                    taskId: null
                },
                taskDelegateDialog:{
                    taskId: null
                },
                taskRejectDialog:{
                    taskId: null
                },
                taskDealWithoutFormDialog:{
                    taskId: null
                }
            }
        },
        methods:{
            showTaskTransferDialog(row){
                this.taskTransferDialog.taskId = row.id
                this.$refs.taskTransferDialog.show()
            },
            showTaskDelegateDialog(row){
                this.taskDelegateDialog.taskId = row.id
                this.$refs.taskDelegateDialog.show()
            },
            showTaskRejectDialog(row){
                this.taskRejectDialog.taskId = row.id
                this.$refs.taskRejectDialog.show()
            },
            showTaskDealWithoutFormDialog(row){
                this.taskDealWithoutFormDialog.taskId = row.id
                this.$refs.taskDealWithoutFormDialog.show()
            },
            taskUnclaim(row){
                this.$confirm('确定要反签收'+ row.name +'吗, 是否继续?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$refs.taskListCom.getTableRef().setOperationButtonLoading(taskUnclaimButtonCode,true)
                    this.axios.post(this.$baseURL.workflow + '/workflow/task/tasks/' + row.id + '/unclaim')
                        .then(res => {
                            this.$message.success('反签收成功')
                            // 重新加载数据
                            this.$refs.taskListCom.getTableRef().refreshData()
                        })
                        .catch(error => {
                            if(error.response){
                                if(error.response.status == 401){
                                    return
                                }
                                if(error.response.status == 404){
                                    this.$message.error('数据不存在，请刷新数据再试')
                                }else {
                                    this.$message.error(error.response.data.msg)
                                }
                            }else {
                                this.$message.error('网络错误或服务不可用')
                            }
                        }).finally(()=>{
                        this.$refs.taskListCom.getTableRef().setOperationButtonLoading(taskUnclaimButtonCode,false)
                    })
                })
            },
            taskResolve(row){
                this.$confirm('确定要归还'+ row.name +'吗, 是否继续?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$refs.taskListCom.getTableRef().setOperationButtonLoading(taskResolveButtonCode,true)
                    this.axios.post(this.$baseURL.workflow + '/workflow/task/tasks/' + row.id + '/resolve')
                        .then(res => {
                            this.$message.success('任务归还成功')
                            // 重新加载数据
                            this.$refs.taskListCom.getTableRef().refreshData()
                        })
                        .catch(error => {
                            if(error.response){
                                if(error.response.status == 404){
                                    this.$message.error('数据不存在，请刷新数据再试')
                                }else {
                                    this.$message.error(error.response.data.msg)
                                }
                            }else {
                                this.$message.error('网络错误或服务不可用')
                            }
                        }).finally(()=>{
                        this.$refs.taskListCom.getTableRef().setOperationButtonLoading(taskResolveButtonCode,false)
                    })
                })
            },
            taskDealWith(row){
                // 先获取表单类型
                this.$refs.taskListCom.getTableRef().setOperationButtonLoading(taskDealWithButtonCode,true)
                this.axios.get(this.$baseURL.workflow + '/workflow/form/task/' + row.id + '/taskFormType')
                    .then((res)=>{
                        let data = res.data
                        switch (data.type) {
                            case 'dynamic':{
                                this.$router.push({path: '/formDynamic', query: {taskId:row.id}})
                                break
                            }
                            case 'relation':{
                                this.$router.push({path: '/formRelation', query: {taskId:row.id}})
                                break
                            }
                            case 'normal':{
                                this.$router.push({path: data.formKey, query: {taskId:row.id,businessKey:row.businessKey}})
                                break
                            }
                            default:{
                                this.showTaskDealWithoutFormDialog(row)
                            }
                        }
                    }).catch(error=>{
                    if(error.response){
                        if(error.response.status == 401){
                            return
                        }
                        this.$message.error(error.response.data.msg)
                    }else {
                        this.$message.error('网络错误或服务不可用')
                    }
                }).finally(()=>{
                    this.formDetailButtonLoading = false
                    // 这里判断一个，有可能页面已经跳转，这里才开始执行，导致为空
                    if(this.$refs.taskListCom){
                        this.$refs.taskListCom.getTableRef().setOperationButtonLoading(taskDealWithButtonCode,false)
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>